package com.opoc.service.Impl;

import com.opoc.mapper.EnterpriseMapper;
import com.opoc.mapper.ReportMapper;
import com.opoc.pojo.Enterprise;
import com.opoc.pojo.Enterprise_sql;
import com.opoc.properties.ExcelProperties;
import com.opoc.service.EnterpriseService;
import com.opoc.utils.ExcelUtils;
import com.opoc.utils.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * @program: opoc
 * @description: 企业管理
 * @author: lyx
 * @create: 2023-10-22 14:41
 */
@Service
@Slf4j

public class EnterpriseServiceImpl implements EnterpriseService {
  @Autowired
  EnterpriseMapper enterpriseMapper;
  @Autowired
  ReportMapper reportMapper;
  //企业基本信息
    @Override
    public List<Enterprise> getEnterpriseBasicInfo(String company_id, String company_name, String belong_region) {
        return enterpriseMapper.getEnterpriseBasicInfo(company_id,company_name,belong_region);
    }

    //添加企业
  public String addEnterprise(Enterprise enterprise){

    Enterprise_sql enterprise_sql = new Enterprise_sql();
    BeanUtils.copyProperties(enterprise, enterprise_sql);

    enterpriseMapper.addEnterprises(enterprise_sql);

    return "access";
  }

    //删除企业
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String deleteEnterprise(List<String> company_ids) {
      for (String company_id:company_ids){
        String pool_id=enterpriseMapper.findPIDbyCID(company_id);
        String admin_id=enterpriseMapper.findAIDbyCID(company_id);
        Enterprise_sql enterprise_sql=enterpriseMapper.getEnterpriseInfoById(company_id);
        if (enterprise_sql!=null){

        //企业文件
          FileUtils.deleteFile(enterprise_sql.getPool_distribution());
          FileUtils.deleteFile(enterprise_sql.getSecurity_manage_net());
        //池子文件
          FileUtils.deleteFile(enterpriseMapper.findPURLbyPID1(pool_id));
          FileUtils.deleteFile(enterpriseMapper.findPURLbyPID2(pool_id));
        //检测报告文件
          FileUtils.deleteFile(reportMapper.URLfindId(company_id));
          //变更请求附件

          enterpriseMapper.deleteEnterprise(company_id);
        }else {
          return "企业不存在";
        }

      }
      return null;
    }
    /**
    * 批量导入企业信息
    * */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String importEnterprise(MultipartFile file) throws Exception {
      Map<String,String> enterpriseHeader=ExcelProperties.EnterpriseHeader;
      Map<String,String> enterpriseHeader_sql=ExcelProperties.EnterpriseHeader_sql;
      List<Enterprise_sql> enterprises=ExcelUtils.getBeansFromExcel(file,enterpriseHeader,enterpriseHeader_sql,Enterprise_sql.class);
      if(enterprises==null){
        return "请求失败，检查文件格式";
      }else{
        for (Enterprise_sql enterprise:enterprises){
          enterpriseMapper.addEnterprises(enterprise);

        }
        return null;
      }
    }

/*
* 导出excel文件
* */
@Override
  public String exportEnterprise(List<String> ids) throws IOException, InvocationTargetException, IllegalAccessException, InstantiationException, NoSuchMethodException {
    List<Enterprise_sql> enterprise_sql=new ArrayList<>();

    for(String id:ids){
      enterprise_sql.add(enterpriseMapper.getEnterpriseInfoById(id));
    }

    //生成excel文件
    String excelPath=ExcelUtils.ExcelGenerator(ExcelProperties.EnterpriseHeader,ExcelProperties.EnterpriseHeader_sql,enterprise_sql,Enterprise_sql.class);
    if(excelPath==null){
      return null;
    }
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
      public void run() {
        FileUtils.deleteFile(excelPath);
      }
    },60 * 1000);

    return FileUtils.getURLFromPath(excelPath);
  }
  /**
   * 添加图片
   */
  @Override
  public Map<String, String> uploadImage(MultipartFile pool_distribution, MultipartFile security_manage_net, String company_id) {
    Map<String, String> result = new HashMap<>();
    String pool_distribution_path = FileUtils.uploadFile(pool_distribution);
    String security_manage_net_path = FileUtils.uploadFile(security_manage_net);
    enterpriseMapper.uploadImage(pool_distribution_path, security_manage_net_path, company_id);
    pool_distribution_path=FileUtils.getURLFromPath(pool_distribution_path);
    security_manage_net_path=FileUtils.getURLFromPath(security_manage_net_path);
    result.put("pool_distribution", pool_distribution_path);
    result.put("security_manage_net", security_manage_net_path);

    return result;
  }
}
